from src.tools.dms_operator import dmsoperator


def excel2md(file_path):

    try:
        excel_tables = dmsoperator.get_excel2md(file_path)
    except Exception as e:
        raise RuntimeError(f"解析 Excel 文件时发生错误: {e}")

    if not excel_tables:
        return ""

    for table in excel_tables:
        table_content = ""
        is_material_table = False
        for _, value in table.items():
            table_content += value + "\n"
            if "物料信息" in value:
                is_material_table = True
        if is_material_table:
            try:
                _table_content = table_content[:table_content.index("|||||")]
                return _table_content
            except ValueError:
                # 如果 "|||||" 不存在，返回整个 table_content
                return table_content
    return ""

if __name__ == '__main__':
    supplier_content_file_id = r"Files\202412\20241216153319Aa9f1FofGA2258e23c93abcc08782951e9019dd34c.xlsx"
    if supplier_content_file_id.endswith('.xlsx'):
        print(excel2md(supplier_content_file_id))

